import os
import base64

def read_file(file_path, offset=0, limit=None):
    if not os.path.exists(file_path):
        raise ValueError("File not found")
    if file_path.endswith(('.png', '.jpg', '.pdf')):  # 多模态
        with open(file_path, 'rb') as f:
            return base64.b64encode(f.read()).decode('utf-8')  # 为AI上传
    else:  # 文本
        with open(file_path, 'r') as f:
            lines = f.readlines()
            if limit:
                lines = lines[offset:offset + limit]
            return ''.join(lines)

def replace_in_file(file_path, old_str, new_str):
    with open(file_path, 'r') as f:
        content = f.read()
    if old_str not in content:
        raise ValueError("Old string not found")
    new_content = content.replace(old_str, new_str)
    with open(file_path, 'w') as f:
        f.write(new_content)
    return "Modified successfully" 

